package com.jingxi.portal.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jingxi.common.pojo.SearchResult;
import com.jingxi.portal.service.SearchService;

@Controller
public class SearchController {

	@Autowired
	private SearchService searchService;
	
	@RequestMapping("/search")
	public void search(@RequestParam("q") String q, Model model,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "16") Integer rows) throws UnsupportedEncodingException {
		q = new String(q.getBytes("ISO-8859-1"), "UTF-8");
		
		SearchResult searchResult = searchService.search("http://localhost:8081/search/query", q, page,rows);
		List list = searchResult.getItemList();
		model.addAttribute("query", q);
		model.addAttribute("itemList", list);
		model.addAttribute("page", searchResult.getCurPage());
		model.addAttribute("rows", "60");
		model.addAttribute("totalPages", searchResult.getPageCount());
	}

}
